package licode.unisop.oidc.auth.modal;

import licode.unisop.oidc.auth.utils.Preconditions;
import lombok.Getter;
import lombok.Setter;

import java.util.Objects;

/**
 * @author licode
 */
@Getter
public class OidcToken {
    private final String tokenType;

    /**
     * 单点登录的会话令牌
     */
    private String ssoToken;

    /**
     * 访问令牌
     */
    private final String accessToken;

    /**
     * 访问令牌的超时时间（单位：毫秒）
     */
    private final Long accessExpiresIn;

    /**
     * 刷新令牌
     */
    private final String refreshToken;

    /**
     * 刷新令牌的超时时间（单位：毫秒）
     */
    private final Long refreshExpiresIn;

    /**
     * 授权范围
     */
    private final String scope;

    /**
     *  获取Oidc的id_token信息
     */
    private String openIdToken;

    /**
     * 上下文信息
     */
    private OidcContext context;

    /**
     * 最后一次续期时间
     */
    @Setter
    private Long lastRenewalTime;

    public OidcToken(String tokenType, String accessToken, Long accessExpiresIn,
                     String refreshToken, Long refreshExpiresIn, String scope) {
        Preconditions.checkNotNull(accessToken, "access_token can't be null");
        this.accessToken = accessToken;
        this.tokenType = tokenType;
        this.accessExpiresIn = accessExpiresIn;
        this.refreshExpiresIn = refreshExpiresIn;
        this.refreshToken = refreshToken;
        this.scope = scope;
    }

    @Override
    public int hashCode() {
        int hash = calcHashCode();
        hash = 37 * hash + Objects.hashCode(openIdToken);
        return hash;
    }

    public void setOpenId(String openId) {
        this.openIdToken = openId;
    }

    public void setSsoToken(String ssoToken) {
        this.ssoToken = ssoToken;
    }

    public void setOpenIdToken(String openIdToken) {
        this.openIdToken = openIdToken;
    }

    public void setContext(OidcContext context) {
        this.context = context;
    }

    private int calcHashCode() {
        int hash = 7;
        hash = 41 * hash + Objects.hashCode(accessToken);
        hash = 41 * hash + Objects.hashCode(tokenType);
        hash = 41 * hash + Objects.hashCode(accessExpiresIn);
        hash = 41 * hash + Objects.hashCode(refreshToken);
        hash = 41 * hash + Objects.hashCode(scope);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final OidcToken other = (OidcToken) obj;
        if (!Objects.equals(accessToken, other.getAccessToken())) {
            return false;
        }
        if (!Objects.equals(tokenType, other.getTokenType())) {
            return false;
        }
        if (!Objects.equals(refreshToken, other.getRefreshToken())) {
            return false;
        }
        if (!Objects.equals(scope, other.getScope())) {
            return false;
        }
        return Objects.equals(accessExpiresIn, other.getAccessExpiresIn());
    }
}
